import {defineStore} from 'pinia'
import {deleteArticleReq, getArticleByIdReq, getArticleListReq, submitArticleReq} from "@/api/article/article.ts";
import {reactive} from "vue";
import {Article, ArticleStatusEnum} from "@/api/article/type.ts";
import {ElMessage} from 'element-plus'
import {formatDate} from "@/utils/time.ts";

export const useArticleStore = defineStore('article', () => {
        return {
            articleInfo: reactive<Article>({
                id: undefined,
                authorId: null,
                content: 'hello',
                status: undefined,
                likes: 0,
                tempId: undefined,
                title: '',
                cover: undefined,
                createTime: '',
                updateTime: '',
            }),
            possibleCovers: reactive({
                selectedCover: '',
                contentCover: '',
                fromMaterialRepository: '',
            }),

            async saveDraft() {
                if (this.articleInfo.title === '') {
                    ElMessage({
                        message: '文章标题不能为空！',
                        type: 'warning',
                    })
                    return Promise.reject('文章标题不能为空！')
                }
                if (this.articleInfo.content === `<p><br></p>` || this.articleInfo.content === '') {
                    ElMessage({
                        message: '文章内容不能为空！',
                        type: 'warning',
                    })
                    return Promise.reject('文章内容不能为空！')
                }
                this.articleInfo.status = ArticleStatusEnum.DRAFT
                submitArticleReq(this.articleInfo).then(res => {
                    console.log('save draft res', res)
                    ElMessage({
                        message: '保存草稿成功！',
                        type: 'success',
                    })
                }).catch(() => {

                })
            },

            async submitArticle() {
                if (this.articleInfo.title === '') {
                    ElMessage({
                        message: '文章标题不能为空！',
                        type: 'warning',
                    })
                    throw await Promise.reject('文章标题不能为空！')
                }
                if (this.articleInfo.content === `<p><br></p>` || this.articleInfo.content === '') {
                    ElMessage({
                        message: '文章内容不能为空！',
                        type: 'warning',
                    })
                    throw await Promise.reject('文章内容不能为空！')
                }
                this.articleInfo.status = ArticleStatusEnum.WAIT_REVIEW
                submitArticleReq(this.articleInfo).then(res => {
                    console.log('submit article res', res)
                    ElMessage({
                        message: '提交文章成功！',
                        type: 'success',
                    })
                }).catch(() => {

                })
            },

            articleList: reactive<Article[]>([]),

            clearArticleInfo() {
                this.articleInfo.id = null
                this.articleInfo.authorId = null
                this.articleInfo.content = ''
                this.articleInfo.status = null
                this.articleInfo.likes = 0
                this.articleInfo.tempId = null
                this.articleInfo.title = ''
                this.articleInfo.cover = null
                this.possibleCovers = {
                    selectedCover: '',
                    contentCover: '',
                    fromMaterialRepository: '',
                }
            },

            async getArticleList() {
                getArticleListReq().then(res => {
                    console.log('get article list res', res)
                    this.articleList = res.data
                    this.articleList.forEach((item) => {
                        // 将时间戳转换为日期格式
                        item.createTime = formatDate(item.createTime)
                        item.updateTime = formatDate(item.updateTime)
                    })
                })
            },

            async deleteArticle(id: number) {
                deleteArticleReq(id).then(res => {
                    console.log('delete article res', res)
                    ElMessage({
                        message: '删除文章成功！',
                        type: 'success',
                    })
                    this.getArticleList();
                })
            },

            async getArticleById(id: any) {
                getArticleByIdReq(id).then(res => {
                    console.log('get article by id res', res)
                    this.setArticleInfo(res.data)
                })
            },

            setArticleInfo(article: Article) {
                this.articleInfo.id = article.id
                this.articleInfo.authorId = article.authorId
                this.articleInfo.content = article.content
                this.articleInfo.status = article.status
                this.articleInfo.likes = article.likes
                this.articleInfo.title = article.title
                this.articleInfo.cover = article.cover
                this.articleInfo.updateTime = article.updateTime
                this.articleInfo.createTime = article.createTime
                this.possibleCovers.selectedCover = article.cover || ''
            }
        }
    },
    {
        persist: true,
    }
)
